package com.xuecheng.ucenter.service;

import com.xuecheng.framework.domain.ucenter.XcCompanyUser;
import com.xuecheng.framework.domain.ucenter.XcMenu;
import com.xuecheng.framework.domain.ucenter.XcUser;
import com.xuecheng.framework.domain.ucenter.ext.XcUserExt;
import com.xuecheng.ucenter.dao.XcMenuMapper;
import com.xuecheng.ucenter.dao.XcUserCompanyRepository;
import com.xuecheng.ucenter.dao.XcUserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    XcUserRepository xcUserRepository;
    @Autowired
    XcUserCompanyRepository xcUserCompanyRepository;
    @Autowired
    XcMenuMapper xcMenuMapper;

    //根据账号查询用户的信息，返回用户扩展信息
    public XcUserExt getUserExt(String username){
        XcUser xcUser=xcUserRepository.findXcUserByUsername(username);
        if(xcUser==null){
            return null;
        }
        List<XcMenu> xcMenus= xcMenuMapper.selectPermissionByUserId(xcUser.getId());
        XcUserExt xcUserExt=new XcUserExt();
        BeanUtils.copyProperties(xcUser,xcUserExt);
        XcCompanyUser xcCompanyUser=xcUserCompanyRepository.findByUserId(xcUser.getId());
        String companyId=null;
        if(xcCompanyUser!=null){
            companyId=xcCompanyUser.getCompanyId();
        }
        xcUserExt.setPermissions(xcMenus);
        xcUserExt.setCompanyId(companyId);
        return xcUserExt;
    }


}
